package com.yykj.hospital.server.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.yykj.common.vo.PageData;
import com.yykj.common.vo.ResultVO;
import com.yykj.hospital.client.dto.DoctorDTO;
import com.yykj.hospital.server.dto.DoctorImportDTO;
import com.yykj.hospital.server.dto.DoctorSignDTO;
import com.yykj.hospital.server.form.DoctorEditForm;
import com.yykj.hospital.server.form.DoctorSearchForm;
import com.yykj.hospital.server.form.ProtoDoctorSearchForm;
import com.yykj.hospital.server.form.ResetDoctorPasswordForm;
import com.yykj.hospital.server.handler.DoctorExcelHandler;
import com.yykj.hospital.server.service.HospitalDoctorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/backstage/hospital/doctor")
@SuppressWarnings("unchecked")
@Api(tags = "医院后台--医生相关接口")
public class HospitalDoctorController {
    @GetMapping("/list")
    @ApiOperation("医生--列表")
    public ResultVO<List<DoctorDTO>> listHospitalDoctor(@RequestParam(value = "hospitalId",required = false) Integer hospitalId,
                                                        @RequestParam(value = "depId",required = false) Integer depId,
                                                        @RequestParam(value = "gender",required = false) String gender,
                                                        @RequestParam(value = "keyword",required = false)  String keyword){
        ProtoDoctorSearchForm searchForm = ProtoDoctorSearchForm.builder()
                .hospitalId(hospitalId)
                .depId(depId)
                .gender(gender)
                .keyword(keyword)
                .build();
        return ResultVO.success(doctorService.getHospitalDoctor(searchForm));
    }


    @GetMapping("/list/page")
    @ApiOperation("医生--列表--有分页")
    public ResultVO<PageData<DoctorDTO>> listHospitalDoctorWithPage(@RequestParam("hospitalId") Integer hospitalId,
                                                                    @RequestParam(value = "depId",required = false) Integer depId,
                                                                    @RequestParam(value = "keyword",required = false)  String keyword,
                                                                    @RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                                                                    @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize
                                                                    ){
        DoctorSearchForm searchForm = DoctorSearchForm.builder()
                .hospitalId(hospitalId)
                .depId(depId)
                .keyword(keyword)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();
        return ResultVO.success(doctorService.listHospitalDoctorWithPage(searchForm));
    }

    @PostMapping("/edit")
    @ApiOperation("修改医生")
    public ResultVO<DoctorDTO> editDoctor(@RequestBody DoctorEditForm doctorEditForm) {
        return ResultVO.success(doctorService.editDoctor(doctorEditForm));
    }

    @GetMapping("/info/{id}")
    @ApiOperation("医生详情")
    public ResultVO<DoctorDTO> info(@PathVariable String id) {
        return ResultVO.success(doctorService.info(id));
    }

    @PostMapping("/reset/password")
    @ApiOperation("医生自己重置密码")
    public ResultVO resetPassword(@RequestBody ResetDoctorPasswordForm form) {
        doctorService.resetSelfPassword(form);
        return ResultVO.success();
    }

    @GetMapping("/template")
    @ApiOperation("导入医生--模板下载 ")
    public ResultVO<String> doctorImportTemplate(){
        return ResultVO.success("https://yykj-1307182424.cos.ap-shanghai.myqcloud.com/static/template/doctor.xlsx");
    }


    @PostMapping("/upload")
    @ApiOperation("导入医生")
    public ResultVO uploadExcel(Integer hospitalId, MultipartFile file ){
        try {
            DoctorExcelHandler doctorExcelHandler = new DoctorExcelHandler();
            ImportParams params = new ImportParams();
            params.setVerifyHandler(doctorExcelHandler);
            params.setNeedVerify(true);
            ExcelImportResult<DoctorImportDTO> result = ExcelImportUtil.importExcelMore(file.getInputStream(), DoctorImportDTO.class, params);
            if (result.isVerifyFail()) {
                return ResultVO.error(result.getFailList());
            } else {
                boolean b = doctorService.checkDoctorList(hospitalId,result);
                if(b){
                    doctorService.importDoctorList(hospitalId,result.getList());
                    return ResultVO.success(result.getList());
                }else{
                    return ResultVO.error(result.getFailList());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new RuntimeException("请按照模板正确填写");
        }
    }


    private HospitalDoctorService doctorService;

    @Autowired
    public void setDoctorService(HospitalDoctorService doctorService) {
        this.doctorService = doctorService;
    }

}
